{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d0005af3-d874-468c-a17a-3772009d29cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from insight import *\n",
    "from insight_tools import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7f56bde0-3227-4e13-92cf-3ad3138b5d5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# filepath = '/Users/shinan/Downloads/VI-Core/visual-insights-lab/ds-cars-service.json'\n",
    "# filepath = '/Users/shinan/Downloads/VI-Core/visual-insights-lab/ds-students-service.json'\n",
    "# filepath = '/Users/shinan/Downloads/VI-Core/visual-insights-lab/input_data.json'\n",
    "# filepath = '/Users/shinan/Downloads/VI-Core/datasets/HumanResourcesDataSet.json'\n",
    "# filepath = '/Users/shinan/Downloads/VI-Core/datasets/COVID-19-Coronavirus.json'\n",
    "# filepath = '/Users/shinan/Downloads/VI-Core/datasets/Inc-5000-Companies.json'\n",
    "# filepath = '/Users/shinan/Downloads/dataset-service.json'\n",
    "filepath = '/Users/shinan/Downloads/test.json'\n",
    "dataSource,fields = read_json(filepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0a3aee24-a2cc-4315-b2c3-ead755d7f4a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "traverse_return = traverse_fields(dataSource,fields)\n",
    "# traverse_fields(dataSource,fields,check_list = ['2DClustering','SimpsonParadoxV2','SimpsonParadoxV1','SimpsonParadoxV3'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aa9d8131-e3df-4b1f-a956-82ce2628640c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# col = ['Acceleration', 'Weight_in_lbs', 'Year', 'Origin']\n",
    "# insight_dict = insight_check(fields,dataSource.loc[:,col],check_list = ['2DClustering','SimpsonParadoxV2','SimpsonParadoxV1','SimpsonParadoxV3'])\n",
    "# display_insight_dict(insight_dict,dataSource)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e6aefce9-c926-4eef-a5ce-b430f93c7ffb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insight_dict = insight_check(fields,dataSource)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ab4ef854-06dc-448d-b9d5-7e5323d74ec9",
   "metadata": {},
   "outputs": [],
   "source": [
    "traverse_return = [traverse_return[i] for i in np.argsort(-np.array([i['dict']['score'] for i in traverse_return]))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8cc02a19-d472-4496-a8e6-3413b7b4335a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for i in traverse_return:\n",
    "#     print([fields['name'][fields['fid']==j].values[0] for j in i['col']])\n",
    "#     print('dimensions:',[fields['name'][fields['fid']==j].values[0] for j in i['insight_input']['dimensions']])\n",
    "#     print('measures:',[fields['name'][fields['fid']==j].values[0] for j in i['insight_input']['measures']])\n",
    "#     print('breakdown:',[fields['name'][fields['fid']==j].values[0] for j in i['insight_input']['breakdown']])\n",
    "#     print('explain:',i['dict']['para']['explain'])\n",
    "#     display_insight_dict({i['type']:i['dict']},dataSource)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9d9406f-51cd-44ad-827c-18e07c94d4e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in traverse_return:\n",
    "    print('explain:',i['dict']['para']['explain'])\n",
    "    display_insight_dict({i['type']:i['dict']},dataSource)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "20d289b7-4f22-44c9-9a7a-d118e2127046",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col_0_53</th>\n",
       "      <th>col_1_57</th>\n",
       "      <th>col_2_83</th>\n",
       "      <th>col_3_22</th>\n",
       "      <th>col_9_13</th>\n",
       "      <th>col_10_43</th>\n",
       "      <th>col_11_91</th>\n",
       "      <th>col_13_78</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SOGZB05371</td>\n",
       "      <td>25/11/2016</td>\n",
       "      <td>Qy1TSDA1ICA=</td>\n",
       "      <td>MDQwICAgICA=</td>\n",
       "      <td>B152935-001</td>\n",
       "      <td>45.0</td>\n",
       "      <td>10</td>\n",
       "      <td>450.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SOAE008441</td>\n",
       "      <td>22/03/2018</td>\n",
       "      <td>Qy1EVDAyICA=</td>\n",
       "      <td>MDI5ICAgICA=</td>\n",
       "      <td>J141-46708</td>\n",
       "      <td>4.0</td>\n",
       "      <td>100</td>\n",
       "      <td>400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SOAB006023</td>\n",
       "      <td>09/09/2016</td>\n",
       "      <td>Qy1QWjAxICA=</td>\n",
       "      <td>MDE4ICAgICA=</td>\n",
       "      <td>NPE20</td>\n",
       "      <td>25.0</td>\n",
       "      <td>10</td>\n",
       "      <td>250.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SOAF009645</td>\n",
       "      <td>30/08/2018</td>\n",
       "      <td>Qy1YTDM2ICA=</td>\n",
       "      <td>MDE3ICAgICA=</td>\n",
       "      <td>PHM1A</td>\n",
       "      <td>122.0</td>\n",
       "      <td>3</td>\n",
       "      <td>366.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SOK0002632</td>\n",
       "      <td>06/09/2016</td>\n",
       "      <td>S0wwNiAgICA=</td>\n",
       "      <td>MDI1ICAgICA=</td>\n",
       "      <td>B814CF2M</td>\n",
       "      <td>187.0</td>\n",
       "      <td>25</td>\n",
       "      <td>4675.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>495</th>\n",
       "      <td>SOAB005056</td>\n",
       "      <td>13/08/2016</td>\n",
       "      <td>Q1MwOCAgICA=</td>\n",
       "      <td>MDI1ICAgICA=</td>\n",
       "      <td>L68124</td>\n",
       "      <td>400.0</td>\n",
       "      <td>1</td>\n",
       "      <td>400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496</th>\n",
       "      <td>SOAE008546</td>\n",
       "      <td>23/03/2018</td>\n",
       "      <td>Q0ctQ0gxMiA=</td>\n",
       "      <td>MDE3ICAgICA=</td>\n",
       "      <td>QBW</td>\n",
       "      <td>140.0</td>\n",
       "      <td>10</td>\n",
       "      <td>1400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>497</th>\n",
       "      <td>SOAA009532</td>\n",
       "      <td>04/03/2016</td>\n",
       "      <td>Q0ctS0wwMyA=</td>\n",
       "      <td>MDE3ICAgICA=</td>\n",
       "      <td>J226-18060</td>\n",
       "      <td>600.0</td>\n",
       "      <td>5</td>\n",
       "      <td>3000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>498</th>\n",
       "      <td>SOAH005395</td>\n",
       "      <td>26/01/2019</td>\n",
       "      <td>Q0ctVEgxMSA=</td>\n",
       "      <td>MDE3ICAgICA=</td>\n",
       "      <td>E580C1-81</td>\n",
       "      <td>165.0</td>\n",
       "      <td>5</td>\n",
       "      <td>825.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>SOAF001786</td>\n",
       "      <td>23/05/2018</td>\n",
       "      <td>Qy1LSzA1ICA=</td>\n",
       "      <td>MDQwICAgICA=</td>\n",
       "      <td>FFA1030Q</td>\n",
       "      <td>230.0</td>\n",
       "      <td>3</td>\n",
       "      <td>690.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       col_0_53    col_1_57      col_2_83      col_3_22     col_9_13  \\\n",
       "0    SOGZB05371  25/11/2016  Qy1TSDA1ICA=  MDQwICAgICA=  B152935-001   \n",
       "1    SOAE008441  22/03/2018  Qy1EVDAyICA=  MDI5ICAgICA=   J141-46708   \n",
       "2    SOAB006023  09/09/2016  Qy1QWjAxICA=  MDE4ICAgICA=        NPE20   \n",
       "3    SOAF009645  30/08/2018  Qy1YTDM2ICA=  MDE3ICAgICA=        PHM1A   \n",
       "4    SOK0002632  06/09/2016  S0wwNiAgICA=  MDI1ICAgICA=     B814CF2M   \n",
       "..          ...         ...           ...           ...          ...   \n",
       "495  SOAB005056  13/08/2016  Q1MwOCAgICA=  MDI1ICAgICA=       L68124   \n",
       "496  SOAE008546  23/03/2018  Q0ctQ0gxMiA=  MDE3ICAgICA=          QBW   \n",
       "497  SOAA009532  04/03/2016  Q0ctS0wwMyA=  MDE3ICAgICA=   J226-18060   \n",
       "498  SOAH005395  26/01/2019  Q0ctVEgxMSA=  MDE3ICAgICA=    E580C1-81   \n",
       "499  SOAF001786  23/05/2018  Qy1LSzA1ICA=  MDQwICAgICA=     FFA1030Q   \n",
       "\n",
       "     col_10_43  col_11_91  col_13_78  \n",
       "0         45.0         10      450.0  \n",
       "1          4.0        100      400.0  \n",
       "2         25.0         10      250.0  \n",
       "3        122.0          3      366.0  \n",
       "4        187.0         25     4675.0  \n",
       "..         ...        ...        ...  \n",
       "495      400.0          1      400.0  \n",
       "496      140.0         10     1400.0  \n",
       "497      600.0          5     3000.0  \n",
       "498      165.0          5      825.0  \n",
       "499      230.0          3      690.0  \n",
       "\n",
       "[500 rows x 8 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSource"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e2f7477b-0b22-46c5-97c4-861317dc2cd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>fid</th>\n",
       "      <th>geoRole</th>\n",
       "      <th>features</th>\n",
       "      <th>semanticType</th>\n",
       "      <th>analyticType</th>\n",
       "      <th>distribution</th>\n",
       "      <th>disable</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>col_3_22</td>\n",
       "      <td>none</td>\n",
       "      <td>{'maxEntropy': 3.584962500721156, 'entropy': 2...</td>\n",
       "      <td>nominal</td>\n",
       "      <td>dimension</td>\n",
       "      <td>[{'memberName': 'MDQwICAgICA=', 'count': 86}, ...</td>\n",
       "      <td>False</td>\n",
       "      <td>f_scode</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>col_9_13</td>\n",
       "      <td>none</td>\n",
       "      <td>{'maxEntropy': 8.74819284958946, 'entropy': 8....</td>\n",
       "      <td>nominal</td>\n",
       "      <td>dimension</td>\n",
       "      <td>[{'memberName': 'B152935-001', 'count': 1}, {'...</td>\n",
       "      <td>False</td>\n",
       "      <td>f_icode</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>col_10_43</td>\n",
       "      <td>none</td>\n",
       "      <td>{'maxEntropy': 4, 'entropy': 3.042962327268187...</td>\n",
       "      <td>quantitative</td>\n",
       "      <td>measure</td>\n",
       "      <td>[{'memberName': 45, 'count': 8}, {'memberName'...</td>\n",
       "      <td>False</td>\n",
       "      <td>f_uprice</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>col_13_78</td>\n",
       "      <td>none</td>\n",
       "      <td>{'maxEntropy': 4, 'entropy': 2.030953034485129...</td>\n",
       "      <td>quantitative</td>\n",
       "      <td>measure</td>\n",
       "      <td>[{'memberName': 450, 'count': 9}, {'memberName...</td>\n",
       "      <td>False</td>\n",
       "      <td>f_iamt</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         fid geoRole                                           features  \\\n",
       "0   col_3_22    none  {'maxEntropy': 3.584962500721156, 'entropy': 2...   \n",
       "1   col_9_13    none  {'maxEntropy': 8.74819284958946, 'entropy': 8....   \n",
       "2  col_10_43    none  {'maxEntropy': 4, 'entropy': 3.042962327268187...   \n",
       "3  col_13_78    none  {'maxEntropy': 4, 'entropy': 2.030953034485129...   \n",
       "\n",
       "   semanticType analyticType  \\\n",
       "0       nominal    dimension   \n",
       "1       nominal    dimension   \n",
       "2  quantitative      measure   \n",
       "3  quantitative      measure   \n",
       "\n",
       "                                        distribution  disable      name  \n",
       "0  [{'memberName': 'MDQwICAgICA=', 'count': 86}, ...    False   f_scode  \n",
       "1  [{'memberName': 'B152935-001', 'count': 1}, {'...    False   f_icode  \n",
       "2  [{'memberName': 45, 'count': 8}, {'memberName'...    False  f_uprice  \n",
       "3  [{'memberName': 450, 'count': 9}, {'memberName...    False    f_iamt  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fields"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8cad190-72a2-42d3-bc82-6927cb7c8189",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'name' in fields.columns:\n",
    "    dataSource.columns = list([fields.loc[fields['fid']==i,'name'].values[0] for i in dataSource.columns])\n",
    "    dimensions = [i for i in dataSource.columns if fields[fields['name']==i]['analyticType'].values=='dimension']\n",
    "    measures = [i for i in dataSource.columns if fields[fields['name']==i]['analyticType'].values=='measure']\n",
    "else:\n",
    "    dimensions = [i for i in dataSource.columns if fields[fields['fid']==i]['analyticType'].values=='dimension']\n",
    "    measures = [i for i in dataSource.columns if fields[fields['fid']==i]['analyticType'].values=='measure']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "df3133d3-e21b-4057-972a-feb6fe646e1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.formula.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c8e2e7c0-050a-42c7-a148-472f13924dc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "m1 = sm.glm(\"col_10_43 ~ col_11_91 + col_13_78\", data=dataSource)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2e041015-bf7b-4568-9dfd-7fcdd9daea7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from clickhouse_driver import connect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f8d919a9-31a1-4981-b666-1f273f277469",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Error on socket shutdown: [Errno 57] Socket is not connected\n"
     ]
    },
    {
     "ename": "OperationalError",
     "evalue": "Code: 81.\nDB::Exception: Database database doesn't exist. Stack trace:\n\n0. DB::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, bool) @ 0xaebed1a in /usr/bin/clickhouse\n1. DB::DatabaseCatalog::assertDatabaseExistsUnlocked(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x146fb31a in /usr/bin/clickhouse\n2. DB::DatabaseCatalog::assertDatabaseExists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x146fb194 in /usr/bin/clickhouse\n3. DB::Context::setCurrentDatabase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x14671bfd in /usr/bin/clickhouse\n4. DB::TCPHandler::runImpl() @ 0x159f10fe in /usr/bin/clickhouse\n5. DB::TCPHandler::run() @ 0x15a03419 in /usr/bin/clickhouse\n6. Poco::Net::TCPServerConnection::start() @ 0x18667a0f in /usr/bin/clickhouse\n7. Poco::Net::TCPServerDispatcher::run() @ 0x18669e61 in /usr/bin/clickhouse\n8. Poco::PooledThread::run() @ 0x1881a549 in /usr/bin/clickhouse\n9. Poco::ThreadImpl::runnableEntry(void*) @ 0x18817c40 in /usr/bin/clickhouse\n10. start_thread @ 0x744b in /usr/lib64/libpthread-2.26.so\n11. clone @ 0xef56f in /usr/lib64/libc-2.26.so\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mServerException\u001b[0m                           Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/dbapi/cursor.py:111\u001b[0m, in \u001b[0;36mCursor.execute\u001b[0;34m(self, operation, parameters)\u001b[0m\n\u001b[1;32m    109\u001b[0m     execute, execute_kwargs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_prepare()\n\u001b[0;32m--> 111\u001b[0m     response \u001b[38;5;241m=\u001b[39m \u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    112\u001b[0m \u001b[43m        \u001b[49m\u001b[43moperation\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mparameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwith_column_types\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m    113\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mexecute_kwargs\u001b[49m\n\u001b[1;32m    114\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    116\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m DriverError \u001b[38;5;28;01mas\u001b[39;00m orig:\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/client.py:304\u001b[0m, in \u001b[0;36mClient.execute\u001b[0;34m(self, query, params, with_column_types, external_tables, query_id, settings, types_check, columnar)\u001b[0m\n\u001b[1;32m    303\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 304\u001b[0m     rv \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess_ordinary_query\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    305\u001b[0m \u001b[43m        \u001b[49m\u001b[43mquery\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwith_column_types\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwith_column_types\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    306\u001b[0m \u001b[43m        \u001b[49m\u001b[43mexternal_tables\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexternal_tables\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    307\u001b[0m \u001b[43m        \u001b[49m\u001b[43mquery_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtypes_check\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtypes_check\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    308\u001b[0m \u001b[43m        \u001b[49m\u001b[43mcolumnar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolumnar\u001b[49m\n\u001b[1;32m    309\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    310\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlast_query\u001b[38;5;241m.\u001b[39mstore_elapsed(time() \u001b[38;5;241m-\u001b[39m start_time)\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/client.py:491\u001b[0m, in \u001b[0;36mClient.process_ordinary_query\u001b[0;34m(self, query, params, with_column_types, external_tables, query_id, types_check, columnar)\u001b[0m\n\u001b[1;32m    489\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection\u001b[38;5;241m.\u001b[39msend_external_tables(external_tables,\n\u001b[1;32m    490\u001b[0m                                      types_check\u001b[38;5;241m=\u001b[39mtypes_check)\n\u001b[0;32m--> 491\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreceive_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43mwith_column_types\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwith_column_types\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    492\u001b[0m \u001b[43m                           \u001b[49m\u001b[43mcolumnar\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolumnar\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/client.py:151\u001b[0m, in \u001b[0;36mClient.receive_result\u001b[0;34m(self, with_column_types, progress, columnar)\u001b[0m\n\u001b[1;32m    148\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mquery_result_cls(\n\u001b[1;32m    149\u001b[0m     gen, with_column_types\u001b[38;5;241m=\u001b[39mwith_column_types, columnar\u001b[38;5;241m=\u001b[39mcolumnar\n\u001b[1;32m    150\u001b[0m )\n\u001b[0;32m--> 151\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mresult\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/result.py:50\u001b[0m, in \u001b[0;36mQueryResult.get_result\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     46\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m     47\u001b[0m \u001b[38;5;124;03m:return: stored query result.\u001b[39;00m\n\u001b[1;32m     48\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m---> 50\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m packet \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpacket_generator:\n\u001b[1;32m     51\u001b[0m     \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstore(packet)\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/client.py:167\u001b[0m, in \u001b[0;36mClient.packet_generator\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    166\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 167\u001b[0m     packet \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreceive_packet\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    168\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m packet:\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/client.py:184\u001b[0m, in \u001b[0;36mClient.receive_packet\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    183\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m packet\u001b[38;5;241m.\u001b[39mtype \u001b[38;5;241m==\u001b[39m ServerPacketTypes\u001b[38;5;241m.\u001b[39mEXCEPTION:\n\u001b[0;32m--> 184\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m packet\u001b[38;5;241m.\u001b[39mexception\n\u001b[1;32m    186\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m packet\u001b[38;5;241m.\u001b[39mtype \u001b[38;5;241m==\u001b[39m ServerPacketTypes\u001b[38;5;241m.\u001b[39mPROGRESS:\n",
      "\u001b[0;31mServerException\u001b[0m: Code: 81.\nDB::Exception: Database database doesn't exist. Stack trace:\n\n0. DB::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, bool) @ 0xaebed1a in /usr/bin/clickhouse\n1. DB::DatabaseCatalog::assertDatabaseExistsUnlocked(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x146fb31a in /usr/bin/clickhouse\n2. DB::DatabaseCatalog::assertDatabaseExists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x146fb194 in /usr/bin/clickhouse\n3. DB::Context::setCurrentDatabase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x14671bfd in /usr/bin/clickhouse\n4. DB::TCPHandler::runImpl() @ 0x159f10fe in /usr/bin/clickhouse\n5. DB::TCPHandler::run() @ 0x15a03419 in /usr/bin/clickhouse\n6. Poco::Net::TCPServerConnection::start() @ 0x18667a0f in /usr/bin/clickhouse\n7. Poco::Net::TCPServerDispatcher::run() @ 0x18669e61 in /usr/bin/clickhouse\n8. Poco::PooledThread::run() @ 0x1881a549 in /usr/bin/clickhouse\n9. Poco::ThreadImpl::runnableEntry(void*) @ 0x18817c40 in /usr/bin/clickhouse\n10. start_thread @ 0x744b in /usr/lib64/libpthread-2.26.so\n11. clone @ 0xef56f in /usr/lib64/libc-2.26.so\n",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mOperationalError\u001b[0m                          Traceback (most recent call last)",
      "Input \u001b[0;32mIn [20]\u001b[0m, in \u001b[0;36m<cell line: 8>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      6\u001b[0m cursor \u001b[38;5;241m=\u001b[39m conn\u001b[38;5;241m.\u001b[39mcursor()\n\u001b[1;32m      7\u001b[0m sql \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mselect fields from database.tableName\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 8\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mcursor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43msql\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/clickhouse_driver/dbapi/cursor.py:117\u001b[0m, in \u001b[0;36mCursor.execute\u001b[0;34m(self, operation, parameters)\u001b[0m\n\u001b[1;32m    111\u001b[0m     response \u001b[38;5;241m=\u001b[39m execute(\n\u001b[1;32m    112\u001b[0m         operation, params\u001b[38;5;241m=\u001b[39mparameters, with_column_types\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m    113\u001b[0m         \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mexecute_kwargs\n\u001b[1;32m    114\u001b[0m     )\n\u001b[1;32m    116\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m DriverError \u001b[38;5;28;01mas\u001b[39;00m orig:\n\u001b[0;32m--> 117\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m OperationalError(orig)\n\u001b[1;32m    119\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_process_response(response)\n\u001b[1;32m    120\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_end_query()\n",
      "\u001b[0;31mOperationalError\u001b[0m: Code: 81.\nDB::Exception: Database database doesn't exist. Stack trace:\n\n0. DB::Exception::Exception(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, bool) @ 0xaebed1a in /usr/bin/clickhouse\n1. DB::DatabaseCatalog::assertDatabaseExistsUnlocked(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x146fb31a in /usr/bin/clickhouse\n2. DB::DatabaseCatalog::assertDatabaseExists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) const @ 0x146fb194 in /usr/bin/clickhouse\n3. DB::Context::setCurrentDatabase(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) @ 0x14671bfd in /usr/bin/clickhouse\n4. DB::TCPHandler::runImpl() @ 0x159f10fe in /usr/bin/clickhouse\n5. DB::TCPHandler::run() @ 0x15a03419 in /usr/bin/clickhouse\n6. Poco::Net::TCPServerConnection::start() @ 0x18667a0f in /usr/bin/clickhouse\n7. Poco::Net::TCPServerDispatcher::run() @ 0x18669e61 in /usr/bin/clickhouse\n8. Poco::PooledThread::run() @ 0x1881a549 in /usr/bin/clickhouse\n9. Poco::ThreadImpl::runnableEntry(void*) @ 0x18817c40 in /usr/bin/clickhouse\n10. start_thread @ 0x744b in /usr/lib64/libpthread-2.26.so\n11. clone @ 0xef56f in /usr/lib64/libc-2.26.so\n"
     ]
    }
   ],
   "source": [
    "host = '44.208.84.235'\n",
    "user = 'default'\n",
    "pw = '123456'\n",
    "database = 'database'\n",
    "conn = connect(f'clickhouse://{user}:{pw}@{host}:9000/{database}')\n",
    "cursor = conn.cursor()\n",
    "sql = \"select fields from database.tableName\"\n",
    "res = cursor.execute(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93e70f61-2a39-4a4f-aa3f-3bda097ec794",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
